package user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import core.constants.Constant;
import core.domain.ListDataInfo;
import core.threadLocal.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user.domain.exam.Exam;
import user.domain.exam.dto.ExamQueryDTO;
import user.domain.exam.dto.ExamRankDTO;
import user.domain.exam.vo.ExamRankVO;
import user.domain.exam.vo.ExamVO;
import user.domain.user.vo.UserDetailVO;
import user.manager.ExamCacheManage;
import user.manager.UserCacheManage;
import user.mapper.CExamMapper;
import user.mapper.CUserExamMapper;
import user.service.IExamService;

import java.util.List;
@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private CExamMapper examMapper;

    @Autowired
    private ExamCacheManage examCacheManage;

    @Autowired
    private CUserExamMapper userExamMapper;

    @Autowired
    private UserCacheManage userCacheManage;

    @Override
    public List<ExamVO> list(ExamQueryDTO examListDTO) {
        PageHelper.startPage(examListDTO.getPageNum(), examListDTO.getPageSize());
        return examMapper.selectExamList(examListDTO);
    }

    @Override
    public ListDataInfo redisList(ExamQueryDTO query) {
        // 从 redis 中获取数据
        Long listSize = examCacheManage.getListSize(query.getType(), null);
        List<ExamVO> examVOList;
        if (listSize == null || listSize == 0) {  // 如果 redis 中没有数据
            examVOList = list(query);   // 从数据库中获取数据
            examCacheManage.refreshCache(query.getType(), null); // 刷新 redis 缓存
            listSize = new PageInfo<>(examVOList).getTotal();
        } else {  // 如果 redis 中有数据
            examVOList = examCacheManage.getExamVOList(query, null); // 从 redis 中获取数据
            listSize = examCacheManage.getListSize(query.getType(), null);
        }
        if(CollectionUtil.isEmpty(examVOList)){
            return ListDataInfo.empty();
        }
        assembleExamVOList(examVOList);
        return ListDataInfo.success(examVOList, listSize.intValue());
    }

    @Override
    public String getFirstQuestion(Long examId) {
        // 获取竞赛中题目列表的顺序 返回第一个题目id
        Long listSize = examCacheManage.getExamQuestionListSize(examId);
        if(listSize == null || listSize == 0){
            examCacheManage.refreshExamQuestionCache(examId);

        }
        return examCacheManage.getFirstQuestion(examId);
    }

    @Override
    public String preQuestion(Long examId, Long questionId) {
        if(examCacheManage.getExamQuestionListSize(examId) <= 0){
            examCacheManage.refreshExamQuestionCache(examId);
        }
        return examCacheManage.preQuestion(examId, questionId).toString();
    }

    @Override
    public String nextQuestion(Long examId, Long questionId) {
        if(examCacheManage.getExamQuestionListSize(examId) <= 0){
            examCacheManage.refreshExamQuestionCache(examId);
        }
        return examCacheManage.nextQuestion(examId, questionId).toString();
    }

    @Override
    public ListDataInfo rankList(ExamRankDTO examRankDTO) {
        Long listSize = examCacheManage.getRankListSize(examRankDTO.getExamId());
        List<ExamRankVO> examRankVOList;
        if (listSize == null || listSize <= 0) {  // 如果 redis 中没有数据
            PageHelper.startPage(examRankDTO.getPageNum(), examRankDTO.getPageSize());
            examRankVOList = userExamMapper.selectExamRankList(examRankDTO.getExamId());
            examCacheManage.refreshExamRankCache(examRankDTO.getExamId()); // 刷新 redis 缓存
            listSize = new PageInfo<>(examRankVOList).getTotal();
        } else {  // 如果 redis 中有数据
            examRankVOList = examCacheManage.getExamRankList(examRankDTO); // 从 redis 中获取数据
            listSize = examCacheManage.getRankListSize(examRankDTO.getExamId());
        }
        if(CollectionUtil.isEmpty(examRankVOList)){
            return ListDataInfo.empty();
        }
        assembleExamRankVOList(examRankVOList);
        return ListDataInfo.success(examRankVOList, listSize.intValue());
    }

    private void assembleExamRankVOList(List<ExamRankVO> examRankVOList) {
        if(CollectionUtil.isEmpty(examRankVOList)){
            return;
        }
        for (ExamRankVO examRankVO : examRankVOList) {
            Long userId = examRankVO.getUserId();
            UserDetailVO user = userCacheManage.getUserById(userId);
            examRankVO.setNickname(user.getNickName());
        }
    }

    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtils.get(Constant.USER_ID, Long.class);
        List<Long> userExamIdList = examCacheManage.getUserAllExamList(userId);
        if(CollectionUtil.isEmpty(userExamIdList)){
            return;
        }
        for (ExamVO examVO : examVOList) {
            if (userExamIdList.contains(examVO.getExamId())) {
                examVO.setEnter(true);
            }
        }

    }

}
